package com.iot08.api.service;

import com.alibaba.fastjson.JSONObject;
import com.iot08.api.config.redis.RedisClusterService;
import com.iot08.api.config.redis.RedisFilterService;
import com.iot08.api.constant.Constant;
import com.iot08.api.response.InquirePhoneChargeResp;
import com.iot08.api.service.supplement.KafkaService;
import com.iot08.api.vo.request.InquirePhoneChargeQueryReqVo;
import com.iot08.api.vo.request.InquirePhoneChargeReqVo;
import com.iot08.common.constant.ConstCacheField;
import com.iot08.common.constant.ConstKafka;
import com.iot08.common.entity.app.InquirePhoneCharge;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.response.ResponseEnum;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author cwj
 * @description
 * @date 2025/3/23 15:33
 **/
@Slf4j
@Service
public class InquirePhoneChargeService extends BaseService<InquirePhoneCharge> {

    @Resource
    private KafkaService kafkaService;

    @Resource
    private RedisClusterService redisClusterService;

    @Resource
    private RedisFilterService redisFilterService;

    /**
     * @description 查询花费流量
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/23 16:03
     **/
    public ResponseMap settingPhoneCharges(InquirePhoneChargeReqVo vo) {
        //检查设备是否在线，不在线不能下发
        String onlineStatus = redisClusterService.hget(vo.getImei(), ConstCacheField.ONLINE_STATUS);
        if (!"1".equals(onlineStatus)) {
            return ResponseMap.fail(ResponseEnum.STATUS5001.getCode(), ResponseEnum.STATUS5001.getMsg());
        }
        //保存或更新查询花费流量的指令
        saveOrUpdateInquirePhoneCharge(vo);
        //设置缓存，用以区分是否为查询话费流量等的短信
        redisFilterService.setex(vo.getImei()+"-"+vo.getSmsNumber()+ Constant.INQUIRE_PHONE_CHARGE_CACHE_POSTFIX,10*60,"1");
        // 下发流量查询
        JSONObject kafkaJson = new JSONObject();
        kafkaJson.put("smsNumber", vo.getSmsNumber());
        kafkaJson.put("smsContent", vo.getSmsContent());
        kafkaService.emitDeviceDemand(vo.getImei(), ConstKafka.PHONE_CHARGE, kafkaJson,vo.getOpenid());
        return ResponseMap.success();
    }

    
    /**
     * @description 查询设备设置的查询话费流量的指令记录
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/23 18:21
     **/
    public ResponseMap queryPhoneCharge(InquirePhoneChargeQueryReqVo vo){
        Query query = new Query();
        query.addCriteria(Criteria.where("imei").is(vo.getImei())).limit(1);
        InquirePhoneChargeResp inquirePhoneChargeResp = mongoTemplate.findOne(query,InquirePhoneChargeResp.class);
        ResponseMap res = ResponseMap.success();
        if(ObjectUtils.isNotEmpty(inquirePhoneChargeResp)) {
            res.put("inquirePhoneChargeResp",inquirePhoneChargeResp);
        }
        return res;
    }

    /**
     * @description 保存或更新查询流量的指令信息ian
     * @author cwj
     * @param [vo]
     * @return void
     * @date @time 2025/3/23 17:55
     **/
    private void saveOrUpdateInquirePhoneCharge(InquirePhoneChargeReqVo vo) {
        InquirePhoneCharge inquirePhoneCharge = this.findOne(new Query(Criteria.where("imei").is(vo.getImei())));
        if(ObjectUtils.isEmpty(inquirePhoneCharge)) {
            inquirePhoneCharge = new InquirePhoneCharge();
            inquirePhoneCharge.setImei(vo.getImei());
            inquirePhoneCharge.setSmsNumber(vo.getSmsNumber());
            inquirePhoneCharge.setSmsContent(vo.getSmsContent());
            inquirePhoneCharge.setCreateTime(new Date());
            inquirePhoneCharge.setUpdateTime(new Date());
            this.save(inquirePhoneCharge);
        } else {
            Update update = new Update();
            update.set("updateTime",new Date());
            update.set("smsNumber", vo.getSmsNumber());
            update.set("smsContent", vo.getSmsContent());
            this.updateById(new ObjectId(inquirePhoneCharge.getId()),update);
        }
    }

}
